package com.ds.infrastructure.audit.collector.config;

import com.ds.infrastructure.audit.collector.interceptor.AccessInterceptor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.PostConstruct;

/**
 * 作用：
 *
 * @author WeiShaoying
 * @date 2020/8/3
 */
@Configuration
@ConfigurationProperties(prefix = "swagger")
@EnableSwagger2
@Data
@Slf4j
public class SwaggerConfig extends WebMvcConfigurationSupport {

    private String basePackage;
    private String developer;
    private String serviceName;
    private String description;
    private String termsOfServiceUrl;
    private String version;
    private boolean enable;

    @Autowired
    private SSOConfig ssoConfig;


    @PostConstruct
    private void init() {
        log.info("##{},{}", basePackage, developer);
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enable).select()
                .apis(RequestHandlerSelectors.basePackage(this.basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(this.serviceName)
                .description(this.description)
                .contact(new Contact(this.developer, "", ""))
                .termsOfServiceUrl(this.termsOfServiceUrl)
                .version(this.version).build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

//    @Bean
//    SSOInterceptor getSSOInterceptor() {
//        return new SSOInterceptor();
//    }

    @Bean
    AccessInterceptor getPublicApiAccessInterceptor() {
        return new AccessInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(this.getSSOInterceptor()).addPathPatterns(this.ssoConfigurationProperties.getPath())
//                .excludePathPatterns(this.ssoConfigurationProperties.getExcludePatterns());
        registry.addInterceptor(this.getPublicApiAccessInterceptor())
                .addPathPatterns("/api/v1/public/audit/queryPositionCode");
        super.addInterceptors(registry);
    }

}
